package ch.zbw.pra.sysventory.presentation.panels;

import java.io.Serializable;
import java.util.List;

import ch.zbw.pra.sysventory.application.controller.ComputerController;
import ch.zbw.pra.sysventory.model.Computer;
import ch.zbw.pra.sysventory.model.HardDisk;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das ComputersArchivePanel
 * 
 * @author Ralph Koster
 */

public class ComputersArchivePanel extends VerticalLayout implements 
											Property.ValueChangeListener, Serializable{

	private static final long serialVersionUID = 4815511618648432891L;
	private ComputerController computerController;
	private Table computersTable;
	private IndexedContainer indexedContainer;
	private HorizontalSplitPanel splitPanel;
	private VerticalLayout leftLayout;
	private VerticalLayout rightLayout;

	public ComputersArchivePanel() {
		this.computerController = new ComputerController();
		paint();
		loadComputers();
	}

	private void paint() {
		this.setCaption("Computer");
		this.setStyleName("computersArchivePanel");
		
		splitPanel = new HorizontalSplitPanel();
		leftLayout = new VerticalLayout();
		rightLayout = new VerticalLayout();
		
		splitPanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		splitPanel.setHeight(100, Sizeable.UNITS_PERCENTAGE);

		paintLeftLayout();

		splitPanel.setSplitPosition(47, Sizeable.UNITS_PERCENTAGE);
		splitPanel.setFirstComponent(leftLayout);
		splitPanel.setSecondComponent(rightLayout);

		this.addComponent(splitPanel);
	}
	
	private void paintLeftLayout() {
		leftLayout.removeAllComponents();
		computersTable = new Table("Scanversionen:");

		indexedContainer = new IndexedContainer();
		indexedContainer = loadComputers();
		
		computersTable.setStyleName("table");
		computersTable.setSizeUndefined();
		computersTable.setHeight(97, Sizeable.UNITS_PERCENTAGE);

		computersTable.setSelectable(true);
		computersTable.setImmediate(true);
		computersTable.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
		computersTable.setColumnCollapsingAllowed(true);
		computersTable.setColumnReorderingAllowed(true);
		computersTable.setContainerDataSource(indexedContainer);
		computersTable.setVisibleColumns(new String[] {
				"Computer Name", "Computer Type",
				"RAM" });
		computersTable.setSelectable(true);
		computersTable.setImmediate(true);
		computersTable.addListener(this);
		
		leftLayout.addComponent(computersTable);
	}

	public IndexedContainer loadComputers() {
		IndexedContainer indexedContainer = new IndexedContainer();
		indexedContainer.addContainerProperty("Computer Id", String.class,
				null);
		indexedContainer.addContainerProperty("Computer Name", String.class,
				null);
		indexedContainer.addContainerProperty("Computer Type", String.class,
				null);
		indexedContainer.addContainerProperty("RAM", String.class,
				null);
		List<Computer> computers = computerController.loadComputers();
		for(Computer computer : computers) {
			int computerId = computer.getId();
			String computerName = computer.getHostNameComputer();
			String computerType = computer.getTypeComputer();
			String computerRam = computer.getRamComputer();
			Object id = indexedContainer.addItem();
			indexedContainer.getContainerProperty(id, "Computer Id")
			.setValue(computerId);
			indexedContainer.getContainerProperty(id, "Computer Name")
			.setValue(computerName);
			indexedContainer.getContainerProperty(id, "Computer Type")
			.setValue(computerType);
			indexedContainer.getContainerProperty(id, "RAM")
			.setValue(computerRam);		
		}
		return indexedContainer;
	}
	
	private void paintComputersInfoPanel(String computerId) {
		rightLayout.removeAllComponents();
		rightLayout.setSizeFull();
		Computer computer = computerController.getComputerById(computerId);
		List<HardDisk> hardDiskList = computer.getHardDisks();
		for(HardDisk hardDisk : hardDiskList) {
			paintHardDiskPanel(hardDisk);
		}
	}

	private void paintHardDiskPanel(HardDisk hardDisk) {
		GridLayout hardDiskPanel = new GridLayout(2, 1);
		GridLayout labelPanel = new GridLayout(1, 3);
		GridLayout textPanel = new GridLayout(1, 3);

		hardDiskPanel.setCaption("Laufwerk " + hardDisk.getDriveLetter());
		hardDiskPanel.addStyleName("hardDiskPanel");
		hardDiskPanel.setSizeUndefined();
		hardDiskPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		
		labelPanel.addComponent(new Label("Laufwerk Grösse:"));
		labelPanel.addComponent(new Label("Freier Speicher:"));
		labelPanel.addComponent(new Label("Auslastung:"));
		textPanel.addComponent(new Label(hardDisk.getHardDiskCapacity()));
		textPanel.addComponent(new Label(hardDisk.getHardDiskFreeSpace()));
		textPanel.addComponent(new Label(hardDisk.getHardDiskEfficiency()));
		
		hardDiskPanel.addComponent(labelPanel);
		hardDiskPanel.addComponent(textPanel);
		
		rightLayout.addComponent(hardDiskPanel);
	}

	public void updateComputersArchivePanel() {
		paintLeftLayout();
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		if (event.getProperty().getValue() != null) {
			String computerId = String.valueOf(computersTable.getItem(
					event.getProperty().getValue()).getItemProperty(
					"Computer Id"));
			paintComputersInfoPanel(computerId);
		}
	}
}
